<template>
  <div class="cart">
    <nav-bar />
    
    <div class="container">
      <h2>购物车</h2>
      
      <div v-if="cartItems.length === 0" class="empty-cart">
        <el-empty description="购物车是空的">
          <el-button type="primary" @click="$router.push('/books')">
            去购物
          </el-button>
        </el-empty>
      </div>
      
      <div v-else>
        <el-table
          :data="cartItems"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          
          <el-table-column label="商品" width="400">
            <template #default="{ row }">
              <div class="product-info">
                <img :src="row.cover" :alt="row.title" class="product-image" />
                <div class="product-details">
                  <h4>{{ row.title }}</h4>
                  <p>{{ row.author }}</p>
                </div>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column label="单价">
            <template #default="{ row }">
              ¥{{ row.price.toFixed(2) }}
            </template>
          </el-table-column>
          
          <el-table-column label="数量">
            <template #default="{ row }">
              <el-input-number
                v-model="row.quantity"
                :min="1"
                :max="row.stock"
                @change="updateQuantity(row)"
              />
            </template>
          </el-table-column>
          
          <el-table-column label="小计">
            <template #default="{ row }">
              ¥{{ (row.price * row.quantity).toFixed(2) }}
            </template>
          </el-table-column>
          
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                type="danger"
                text
                @click="removeFromCart(row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <div class="cart-footer">
          <div class="cart-summary">
            <div class="selected-count">
              已选择 {{ selectedItems.length }} 件商品
            </div>
            <div class="total-amount">
              总计：<span class="amount">¥{{ totalAmount.toFixed(2) }}</span>
            </div>
          </div>
          
          <el-button
            type="primary"
            size="large"
            :disabled="selectedItems.length === 0"
            @click="checkout"
          >
            结算 ({{ selectedItems.length }})
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import NavBar from '@/components/NavBar.vue'
import { ElMessage } from 'element-plus'

const cartItems = ref([
  {
    id: 1,
    title: '深入理解计算机系统',
    author: 'Randal E. Bryant',
    price: 139.00,
    cover: 'https://via.placeholder.com/100x150/667eea/ffffff?text=CSAPP',
    quantity: 1,
    stock: 15
  },
  {
    id: 2,
    title: '算法导论',
    author: 'Thomas H. Cormen',
    price: 128.00,
    cover: 'https://via.placeholder.com/100x150/764ba2/ffffff?text=算法导论',
    quantity: 2,
    stock: 10
  },
  {
    id: 3,
    title: 'Python编程：从入门到实践',
    author: 'Eric Matthes',
    price: 89.00,
    cover: 'https://via.placeholder.com/100x150/f5576c/ffffff?text=Python',
    quantity: 1,
    stock: 20
  }
])

const selectedItems = ref([])

const totalAmount = computed(() => {
  return selectedItems.value.reduce((total, item) => {
    return total + (item.price * item.quantity)
  }, 0)
})

const handleSelectionChange = (selection) => {
  selectedItems.value = selection
}

const updateQuantity = (item) => {
  console.log('更新数量:', item)
}

const removeFromCart = (item) => {
  const index = cartItems.value.findIndex(cartItem => cartItem.id === item.id)
  if (index > -1) {
    cartItems.value.splice(index, 1)
    ElMessage.success('已从购物车删除')
  }
}

const checkout = () => {
  console.log('结算商品:', selectedItems.value)
  ElMessage.success('正在跳转到结算页面')
}
</script>

<style scoped>
.cart {
  min-height: 100vh;
  background: #f5f5f5;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

h2 {
  margin-bottom: 30px;
  color: #333;
}

.empty-cart {
  display: flex;
  justify-content: center;
  padding: 60px 0;
}

.product-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.product-image {
  width: 80px;
  height: 120px;
  object-fit: cover;
  border-radius: 4px;
}

.product-details h4 {
  margin: 0 0 5px 0;
  font-size: 16px;
}

.product-details p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 30px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.cart-summary {
  display: flex;
  align-items: center;
  gap: 30px;
}

.selected-count {
  color: #666;
}

.total-amount {
  font-size: 18px;
}

.amount {
  color: #ff6b6b;
  font-weight: bold;
  font-size: 24px;
}
</style>
